/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
package seedpod.webapp;


import java.util.HashMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class LimsContext extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * parameter strings defined the context web.xml
	 */

	public static final String PARAM_LOGIN_URL = "loginURL";
	public static final String PARAM_SEEDPOD_STYLESHEET = "seedpod_default_stylesheet";
	public static final String PARAM_APPLICATION_DIRECTORY = "applicationDirectory";
	public static final String PARAM_LIMS_NAME = "LIMSName";
	public static final String PARAM_DATABASE_DRIVER = "DatabaseDriver";
	public static final String PARAM_DATABASE_URL = "DatabaseURL";
	public static final String PARAM_DATABASE_USER = "DatabaseUser";
	public static final String PARAM_DATABASE_PASSWORD = "DatabasePassword";
	public static final String PARAM_DATABASE = "DatabaseName";
	public static final String PARAM_KNOWLEDGE_BASE_PATH = "KnowledgeBasePath";
	public static final String PARAM_APPLICATION_ADMIN_NAME = "ApplicationAdmin";
	public static final String PARAM_APPLICATION_ADMIN_PASSWORD = "ApplicationAdminPassword";
	public static final String PARAM_WEBAPP_URL_PATH = "WebappURL";

	/** this page is not in the application directory * */
	private static String DEFAULT_LOGIN_URL = "/signin.jsp";

	public static final String LOGOUT_URL = "logout";
	private static final String USER_PAGE_DIRECTORY = "pages";

	/** Anything beyond this directory is for authentication * */
	private static String DEFAULT_APPLICATION_DIRECTORY = "/lims/";
	private static String DEFAULT_LIMS_NAME = "SeedPod LIMS";
	public static final String HOME_URL = DEFAULT_APPLICATION_DIRECTORY  + "index.jsp";

	private static ServletContext _context;
	private static HashMap<String, ContextParameter> _paramHash;
	
	public void init( ServletConfig config ) throws ServletException {
		_paramHash = new HashMap<String, ContextParameter>();
		_context = config.getServletContext();

		// variables with default values
		initParam( PARAM_LOGIN_URL, DEFAULT_LOGIN_URL );
		initParam( PARAM_APPLICATION_DIRECTORY, DEFAULT_APPLICATION_DIRECTORY );
		initParam( PARAM_LIMS_NAME, DEFAULT_LIMS_NAME );
		initParam( PARAM_SEEDPOD_STYLESHEET );

		// required database connect variables
		initParam( PARAM_DATABASE_DRIVER );
		initParam( PARAM_DATABASE_URL );
		initParam( PARAM_DATABASE_USER );
		initParam( PARAM_DATABASE_PASSWORD );
		initParam( PARAM_DATABASE );

		initParam( PARAM_KNOWLEDGE_BASE_PATH );
		initParam( PARAM_WEBAPP_URL_PATH );
		initParam( PARAM_APPLICATION_ADMIN_NAME );
		initParam( PARAM_APPLICATION_ADMIN_PASSWORD );
	}

	private ContextParameter initParam( String key ) throws ServletException {
		return initParam( key, null );
	}

	private ContextParameter initParam( String key, String defaultVal ) throws ServletException {
		String value = getServletInitParameter( key );

		if ( value == null ) {
			String exception = "Missing Servlet Context parameter " + key;
			log( exception );
			throw new ServletException( exception );
		}

		ContextParameter p = new ContextParameter( key, value, defaultVal );
		_paramHash.put( key, p );
		return p;
	}

	public static String getContextParameter( String key ) {
		ContextParameter p = (ContextParameter)_paramHash.get( key );
		if ( p != null )
			return (p.value());
		else
			return null;
	}
	
	public static String getRealPath( String file ) {
		return _context.getRealPath( file );
	}

	private String getServletInitParameter( String paramName ) {
		return _context.getInitParameter( paramName );
	}

	public static String getLoginUrl() {
		return LimsContext.getContextParameter( PARAM_LOGIN_URL );
	}

	public static String getLimsName() {
		return LimsContext.getContextParameter( PARAM_LIMS_NAME );
	}

	private class ContextParameter {
		private String _key;
		private String _val;
		private String _defaultVal = null;

		public ContextParameter(String key, String value, String defaultValue) {
			_key = key;
			_val = value;
			_defaultVal = defaultValue;
		}

		public ContextParameter(String key, String value) {
			this( key, value, null );
		}

		public String key() {
			return _key;
		}

		public String value() {
			if ( _val != null )
				return _val;
			else
				return _defaultVal;
		}

		public Integer integerValue() {
			return Integer.parseInt( value() );
		}

		public String defaultValue() {
			return _defaultVal;
		}

		public boolean equals( ContextParameter o ) {
			return (this._key.equals( o._key ) && this._val.equals( o._val ));
		}

	}

	public static String getHomeUrl() {
		return HOME_URL;
	}

	public static String getLogoutUrl() {
		return LOGOUT_URL;
	}

	public static String getUserPageDirectory() {
		return USER_PAGE_DIRECTORY;
	}

}
